package com.ruoyi.web.controller.boc;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.boc.domain.BocAuthInfo;
import com.ruoyi.boc.service.IBocAuthInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 授权信息Controller
 * 
 * @author ruoyi
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/boc/authInfo")
public class BocAuthInfoController extends BaseController
{
    @Autowired
    private IBocAuthInfoService bocAuthInfoService;

    /**
     * 查询授权信息列表
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BocAuthInfo bocAuthInfo)
    {
        startPage();
        if(getUserId() != 1) {
            bocAuthInfo.setApplyId(getUserId());
        }
        List<BocAuthInfo> list = bocAuthInfoService.selectBocAuthInfoList(bocAuthInfo);
        return getDataTable(list);
    }

    /**
     * 导出授权信息列表
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:export')")
    @Log(title = "授权信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BocAuthInfo bocAuthInfo)
    {
        List<BocAuthInfo> list = bocAuthInfoService.selectBocAuthInfoList(bocAuthInfo);
        ExcelUtil<BocAuthInfo> util = new ExcelUtil<BocAuthInfo>(BocAuthInfo.class);
        util.exportExcel(response, list, "授权信息数据");
    }

    /**
     * 获取授权信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:query')")
    @GetMapping(value = "/{authId}")
    public AjaxResult getInfo(@PathVariable("authId") Long authId)
    {
        return success(bocAuthInfoService.selectBocAuthInfoByAuthId(authId));
    }

    /**
     * 新增授权信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:add')")
    @Log(title = "授权信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BocAuthInfo bocAuthInfo)
    {
        bocAuthInfo.setApplyId(getUserId());
        bocAuthInfo.setCreateBy(getUsername());
        return toAjax(bocAuthInfoService.insertBocAuthInfo(bocAuthInfo));
    }

    /**
     * 修改授权信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:edit')")
    @Log(title = "授权信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BocAuthInfo bocAuthInfo)
    {
        BocAuthInfo tmp = bocAuthInfoService.selectBocAuthInfoByAuthId(bocAuthInfo.getAuthId());
        if(!tmp.getStatus().equals("1")){
            return error("当前状态不允许修改");
        }
        return toAjax(bocAuthInfoService.updateBocAuthInfo(bocAuthInfo));
    }

    /**
     * 删除授权信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:remove')")
    @Log(title = "授权信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{authIds}")
    public AjaxResult remove(@PathVariable Long[] authIds)
    {
        return toAjax(bocAuthInfoService.deleteBocAuthInfoByAuthIds(authIds));
    }


    /**
     * 查询授权信息列表
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:checkList')")
    @GetMapping("/checkList")
    public TableDataInfo checkList(BocAuthInfo bocAuthInfo)
    {
        startPage();
        if(getUserId() != 1) {
            bocAuthInfo.setCheckId(getUserId());
        }
        List<BocAuthInfo> list = bocAuthInfoService.selectBocAuthInfoList(bocAuthInfo);
        return getDataTable(list);
    }

    /**
     * 查询授权信息列表
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:empowerList')")
    @GetMapping("/empowerList")
    public TableDataInfo empowerList(BocAuthInfo bocAuthInfo)
    {
        startPage();
        if(getUserId() != 1) {
            bocAuthInfo.setEmpowerId(getUserId());
        }
        List<BocAuthInfo> list = bocAuthInfoService.selectBocAuthInfoList(bocAuthInfo);
        return getDataTable(list);
    }

    /**
     * 修改授权信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:check')")
    @Log(title = "授权信息", businessType = BusinessType.UPDATE)
    @PutMapping("/check")
    public AjaxResult check(@RequestBody BocAuthInfo bocAuthInfo)
    {
        BocAuthInfo tmp = bocAuthInfoService.selectBocAuthInfoByAuthId(bocAuthInfo.getAuthId());
        if(!tmp.getStatus().equals("1")){
            return error("当前状态不允许复核");
        }
        Date now = new Date();
        bocAuthInfo.setCheckTime(new java.sql.Date(now.getTime()));
        return toAjax(bocAuthInfoService.updateBocAuthInfo(bocAuthInfo));
    }

    /**
     * 修改授权信息
     */
    @PreAuthorize("@ss.hasPermi('boc:authInfo:empower')")
    @Log(title = "授权信息", businessType = BusinessType.UPDATE)
    @PutMapping("/empower")
    public AjaxResult empower(@RequestBody BocAuthInfo bocAuthInfo)
    {
        BocAuthInfo tmp = bocAuthInfoService.selectBocAuthInfoByAuthId(bocAuthInfo.getAuthId());
        if(!tmp.getStatus().equals("2")){
            return error("当前状态不允许授权");
        }
        Long checkId = tmp.getCheckId();
        Long empowerId = bocAuthInfo.getEmpowerId();
        if(checkId == empowerId){
            return error("复核和授权人不能相同");
        }
        Date now = new Date();
        bocAuthInfo.setEmpowerTime(new java.sql.Date(now.getTime()));
        return toAjax(bocAuthInfoService.updateBocAuthInfo(bocAuthInfo));
    }
}
